<?php
/**
 * 
 * @author bqd
 *
 */

class ExtendedCacheBehavior extends ModelBehavior {
	var $options = array();
	var $cacheName = '';
    function setup(&$model, $settings = array()) {
        $default = array(
        	'defaultCacheList'		=>	array(),
        	'cache_config'	=> array(  
			    'engine' => 'File',  
			    'duration'=> '+6 hours',  
			    'path' => CACHE,  
			    'prefix' => "cache_{$model->name}_"
			)
       	);
	    if (!isset($this->__settings[$model->alias])) {
			$this->__settings[$model->alias] = $default;
		}

       	$this->__settings[$model->alias] = am($this->__settings[$model->alias], $settings);
		$this->cacheConfig = $this->__settings[$model->alias]['cache_config'];
       	
       	if (!isset($this->__settings[$model->alias]['clearOnCreate'])) {
       		$this->__settings[$model->alias]['clearOnCreate'] = $this->__settings[$model->alias]['defaultCacheList'];
       	}
    	if (!isset($this->__settings[$model->alias]['clearOnUpdate'])) {
       		$this->__settings[$model->alias]['clearOnUpdate'] = $this->__settings[$model->alias]['defaultCacheList'];
       	}
    	if (!isset($this->__settings[$model->alias]['clearOnDelete'])) {
       		$this->__settings[$model->alias]['clearOnDelete'] = $this->__settings[$model->alias]['defaultCacheList'];
       	}
       	
       	Cache::set($this->__settings[$model->alias]['cache_config']);
    }
    
    /**
     * (non-PHPdoc)
     * @see cake/libs/model/ModelBehavior::afterSave()
     */
    function afterSave(&$model, $created) {
    	if ($created) {
    		$this->clearCache($model, $this->__settings[$model->alias]['clearOnCreate']);
    	} else {
    		$this->clearCache($model, $this->__settings[$model->alias]['clearOnUpdate']);
    	}
    }
    
    /**
     * (non-PHPdoc)
     * @see cake/libs/model/ModelBehavior::afterDelete()
     */
    function afterDelete(&$model) {
		$this->clearCache($model, $this->__settings[$model->alias]['clearOnDelete']);
    }
    
    /**
     * 
     * Phương thức được sử dụng để xóa tất cả các cache trong danh sách
     * @param $cacheList
     */
    private function clearCache($model, $cacheList) {
    	$languages = Configure::read('Config.languages');
    	foreach ($cacheList as $item) {
    		foreach ($languages as $lang) {
    			Cache::delete("{$lang}_{$this->__settings[$model->alias]['cache_config']['prefix']}{$item}");
    		}
    	}
    }
    
    function beforeFind(&$model, $queryData) {
    	// Kiểm tra xem đã có Cache chưa
    	if (isset($queryData['extendedCache'])) {
    		$this->cacheName[$model->alias] = $queryData['extendedCache'];
    		$this->cache[$model->alias] = Cache::read("{$model->locale}_{$this->__settings[$model->alias]['cache_config']['prefix']}{$this->cacheName[$model->alias]}");
    		
    		if ($this->cache[$model->alias] !== false) {
    			$model->data = $this->cache[$model->alias];
    			return false;
    		} else {
    			return true;
    		}
    	} else {
    		return true;
    	}
    }
    function afterFind(&$model, $results, $primary) {
    	if ($primary) {
    		if (!empty($this->cacheName[$model->alias])) {
	    		Cache::write("{$model->locale}_{$this->__settings[$model->alias]['cache_config']['prefix']}{$this->cacheName[$model->alias]}", $results);
    		}
    	}
    	return $results;
    }
}
?>